package org.br.erp.base.extendtab.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.extendtab.entity.Scmextendtab;
import org.br.erp.base.extendtab.mapper.ScmextendtabMapper;
import org.br.erp.base.extendtab.service.IScmextendtabService;
import org.br.erp.base.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.sql.DataSource;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 扩展表
 * @Author: jeecg-boot
 * @Date:   2023-11-02
 * @Version: V1.0
 */
@Service
public class ScmextendtabServiceImpl extends ServiceImpl<ScmextendtabMapper, Scmextendtab> implements IScmextendtabService {


    @Autowired
    ScmextendtabMapper scmextendtabMapper;

    @Autowired
    SpringUtils springUtils;

    @Override
    public void executeExtendJar(String objectCode, String extendPoint, HashMap params) {



        LambdaQueryWrapper<Scmextendtab> abstractLambdaWrapper=new LambdaQueryWrapper<>();
        abstractLambdaWrapper.eq(Scmextendtab::getObjectcode,objectCode);
        abstractLambdaWrapper.eq(Scmextendtab::getExtendpoint,extendPoint);
        abstractLambdaWrapper.ne(Scmextendtab::getUseflag,"N");
        abstractLambdaWrapper.orderByAsc(Scmextendtab::getOrdernum);
        List<Scmextendtab> scmextendtabs = scmextendtabMapper.selectList(abstractLambdaWrapper);
        if(scmextendtabs.size()==0){
            return;
        }

        for (Scmextendtab scmextendtab : scmextendtabs) {
            String extendjar = scmextendtab.getExtendjar();
            String extendmethodname = scmextendtab.getExtendmethodname();
            String extendcalssname = scmextendtab.getExtendclassname();
            try {
                File file = new File(this.getClass().getClassLoader().getResource("").getPath() + extendjar);
                URL url = file.toURI().toURL();
                ClassLoader classLoader = new URLClassLoader(new URL[]{url});
                Class<?> aClass = classLoader.loadClass(extendcalssname);

                Object jarObject = aClass.newInstance();
                Method method = aClass.getMethod(extendmethodname, HashMap.class);
                method.invoke(jarObject,params);

//                HashMap<String, Object> paramMap = new HashMap<>();
//                paramMap.put("param", params);
//                try {
//                    springUtils.invokeService(extendjar, extendmethodname, paramMap);
//                } catch (InvocationTargetException exception) {
//                    throw new RuntimeException(exception.getTargetException().getMessage());
//                } catch (Exception e) {
//                    throw new RuntimeException(e.getMessage());
//                }
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }
    }
}
